package com.mycode.ww.service;

import com.alibaba.fastjson.JSONObject;
import com.mycode.ww.util.CommonUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

/**
 * @author ww
 * @date 2018/7/17
 */
@Service
public class LoginService {
    /**
     * 登录表单提交
     *
     * @param jsonObject
     * @return
     */
    public JSONObject authLogin(JSONObject jsonObject) {
        String username = jsonObject.getString("username");
        String password = jsonObject.getString("password");
        System.out.println("loginService username:" + username + " password:" + password);
        JSONObject returnData = new JSONObject();
        Subject currentUser = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try {
            currentUser.login(token);
            returnData.put("result", "success");
        } catch (AuthenticationException e) {
            returnData.put("result", "fail");
        }
        return CommonUtil.successJson(returnData);
    }
}
